$(function (){
    //初始化数据库
    initDataSource();
    $("#tables").selectpicker('refresh');
    initEvent();
})


function initEvent(){
    $(".generate-box").on("change","#dataSourceCode",function (){
       var val = $(this).val();
       if(!val){
           return;
       }
        initDataSourceTable(val);
    });

    $(".generate-box").on("click",".generate",function (){

        var json = {dataSource:{},strategy:{}};
        json.dataSource.dataSourceCode = $("#dataSourceCode").val();
        json.strategy.tables = $("#tables").val();
        json.author = $("#author").val();
        json.javaParent = $("#javaParent").val();
        json.javaRootPath = $("#javaRootPath").val();
        json.xmlRootPath = $("#xmlRootPath").val();

        var rows = $("#strategy tbody tr");
        for(var i = 0 ; i < rows.length ;i ++){
            var row = $(rows[i]);
            var type =  row.find("input[name='type']").val();
            var name =  row.find("input[name='name']").val();
            var package = row.find("input[name='package']").val();
            var disable = row.find("input[name='disable']").prop("checked");
            json.strategy[type] = {fileName:name,packageName:package,disable:!disable};
        }
        $.ajax({
            url: "/generator/generate",
            data: JSON.stringify(json),
            method: "post",
            contentType: 'application/json',
            xhrFields: {
                responseType: "blob"
            },
            success: function (data, status, xhr) {
                var  contentDisposition = xhr.getResponseHeader("Content-Disposition");
                var filename = contentDisposition.replace('attachment;filename=', '');
                var link = document.createElement('a'); // 创建一个a标签
                var url = window.URL.createObjectURL(data); // 将Blob对象转换为可下载的链接
                link.href = url;
                link.download = filename; // 设置下载的文件名
                link.click(); // 模拟点击链接进行下载
                window.URL.revokeObjectURL(url);
            }
        });
    });

}

function initDataSource() {
    $.ajax({
        url: "/datasource/query",
        async : false,
        success: function (data) {
            if(!data){return;}
            initDataSourceRender(data);
        }
    });
}

function initDataSourceRender (data) {
    var opts = [];
    opts.push("<option value=''>请选择</option>");
    for(var i = 0 ; i < data.length ; i++){
        opts.push("<option value='"+data[i].id+"'>"+data[i].code+"</option>");
    }
    var $ds = $("#dataSourceCode");
    $ds.html(opts.join(""));
    $ds.selectpicker('refresh');
}

function initDataSourceTable(dataSourceId) {
    $.ajax({
        url: "/datasource/queryTable",
        data:  {"id":dataSourceId},
        method: "post",
        dataType: "json",
        success: function (data) {
            if(!data){ return ;}
            initTableRender(data);
        }
    });
}

function initTableRender (data) {
    var opts = [];
    opts.push("<option value=''>请选择</option>");
    for(var i = 0 ; i < data.length ; i++){
        opts.push("<option value='"+data[i]+"'>"+data[i]+"</option>");
    }
    var $ds = $("#tables");
    $ds.html(opts.join(""));
    $ds.selectpicker('refresh');
}